<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 3.2.&nbsp; Coding Building Blocks</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-3-SECT-2"></a>
<h3 id="631368-992" class="docSection1Title">3.2. Coding Building Blocks</h3>
<p class="docText">To write programs in PHP<a name="IDX-CHP-3-0057"></a> 
 that do something useful, you'll need to understand blocks of reusable code called <span class="docEmphasis">functions</span> or <span class="docEmphasis">methods</span>, and then how to temporarily store information that cannot be executed in <span class="docEmphasis">variables</span>.<a name="IDX-CHP-3-0058"></a> 
 We talk about <span class="docEmphasis">evaluations</span>, which are basically things that allow your code to make intelligent decisions based on mathematical principles and user input.</p>
<a name="learnphpmysql-CHP-3-SECT-2.1"></a>
<h4 id="title-IDAWO1ME" class="docSection2Title">3.2.1. Variables</h4>
<p class="docText">Since you haven't done any programming, we understand that variables are a new concept. A <span class="docEmphasis">variable</span> stores a value, such as the text string "Hello World!" or the integer value 1. A variable can then be reused throughout your code, instead of having to type out the actual value over and over again for the entire life of the variable, which can be frustrating and tedious. <a class="docLink" href="#learnphpmysql-CHP-3-FIG-2">Figure 3-2</a> shows a newly created variable that has been assigned a value of 30.</p>
<a name="learnphpmysql-CHP-3-FIG-2"></a><p><center>
<h5 class="docFigureTitle">Figure 3-2. A variable holding a value</h5>
<img border="0" alt="" id="195131084199" width="517" height="155" SRC="images/learnphpmysql_0302.jpg">
</center></p><br>
<p class="docText">In PHP, you define a variable with the following form:</p>
<pre>
$variable_name = value;
</pre><BR>

<p class="docText">Pay very close attention to some key elements in the form of variables. The dollar sign (<tt>$</tt>) must always fill the first space of your variable. The first character after the dollar sign must be a letter or underscore. It can't under any circumstances be a number; otherwise, your code will not execute, so watch those typos!</p>
<ul><LI><p class="docList">PHP variables may only be composed of alphanumeric characters and underscores; for example, <tt>a</tt>-<tt>z, A</tt>-<tt>Z, 0</tt>-<tt>9</tt>, and <tt>_</tt>.</P></li><LI><p class="docList">Variables in PHP are case sensitive. This means that <tt>$variable_name</tt> and <tt>$Variable_Name</tt> are different.</p></li><li><p class="docList">Variables with more than one word should be separated with underscores; for example, <tt>$test_variable</tt>.</P></li><li><p class="docList">Variables can be assigned values by using the equals sign (<tt>=</tt>).</P></li><li><p class="docList">Always end with a semicolon (<tt>;</tt>) to complete the assignment of the variable.</P></li></ul>
<p class="docText">To create a simple PHP variable as in <a class="docLink" href="#learnphpmysql-CHP-3-FIG-2">Figure 3-2</a>, enter:</p>
<pre>
&lt;?php
$age = 30;
?&gt;
</pre><br>

<p class="docText">This code takes the variable named <tt>age</tt> and assigns it the number 30. You can use variables without worrying about the specific value assigned to them. (PHP is not strongly typed, if you have a background in Java or C.)</P>
<p class="docText">If you were to assign a new value to a variable with the same name, as happens in <a class="docLink" href="#learnphpmysql-CHP-3-EX-6">Example 3-6</a>, the old name would be overwritten and you would have a potential logic error in your code.</P>
<a name="learnphpmysql-CHP-3-EX-6"></a><h5 id="title-IDABS1ME" class="docExampleTitle">Example 3-6. Reassigning a variable</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
 $age = 30;
 $age = 31;
 echo $age;
?&gt;
</pre><br>

</TD></tr></table></P>
<p class="docText">The new value of <tt>$age</tt> replaces the old, and this is the output:</P>
<pre>
31
</pre><BR>

<a name="learnphpmysql-CHP-3-SECT-2.1.1"></a>
<h5 id="title-IDARS1ME" class="docSection3Title">3.2.1.1. Reading a variable's value</h5>
<a name="IDX-CHP-3-0059"></a> 
<a name="IDX-CHP-3-0060"></a> 
<a name="IDX-CHP-3-0061"></a> 

<p class="docText">To access the value of a variable that's already been assigned, simply specify the dollar sign (<tt>$</tt>) followed by the variable name, and use it as you would the value of the variable in your code. For example, <a class="docLink" href="#learnphpmysql-CHP-3-FIG-3">Figure 3-3</a> displays the value of <tt>$age</tt>.</p>
<p class="docText">You don't necessarily have to clean up your variables when your program finishes. They're temporary, since PHP automatically cleans them up when you're done using them. Sort of like how Microsoft Word creates a temp file of your document, so when you close the document, the temp file deletes itself.</p>

<a name="learnphpmysql-CHP-3-SECT-2.1.2"></a>
<h5 id="title-IDAXT1ME" class="docSection3Title">3.2.1.2. Variable types</h5>
<a name="IDX-CHP-3-0062"></a> 
<a name="IDX-CHP-3-0063"></a> 

<p class="docText">Variables all store certain types of data. PHP automatically picks a data variable based on the value assigned. These data types include strings, numbers, and more complex types such as arrays. We'll discuss arrays later. What's important to know is that unless you have a reason to care about the data type, PHP handles all of the details, so you need not worry about that. However, it's good practice to learn about data types.</p>
<p class="docText">In situations where a specific type of data is required, such as the mathematical division operation, PHP attempts to convert the data types automatically. If you have a string with a single "2," it will be converted to an integer value of 2. This conversion is nearly always exactly what you want PHP to do and makes coding seamless for you.</p>

<a name="learnphpmysql-CHP-3-SECT-2.1.3"></a>
<h5 id="title-IDAOU1ME" class="docSection3Title">3.2.1.3. Variable scope</h5>
<a name="IDX-CHP-3-0064"></a> 
<a name="IDX-CHP-3-0065"></a> 

<p class="docText">PHP<a name="IDX-CHP-3-0066"></a> 
 helps keep your code organized by making sure that, if you use code that someone else wrote (and you very likely will), the names of the variables in your code don't clash with other previously written variable names. For example, if you're using a variable called <tt>$name</tt> that has a value of <tt>Bill</tt>, and you use someone else's code that also has a variable called <tt>$name</tt> but uses it to keep track of the filename <span class="docEmphasis">log.txt</span>, your value could get overwritten. Your code's value for <tt>$name</tt> of <tt>Bill</tt> will be replaced by <tt>log.txt</tt>, and your code will say <tt>"Hello log.txt"</tt> instead of <tt>"Hello Bill"</tt>, which would be a big problem.</p>
<p class="docText">To solve this problem, PHP organizes code into <span class="docEmphasis">functions</span>. Functions allow you to group a chunk of code together and execute that code by its name. To keep variables in your code separate from variables in functions, PHP provides separate storage of variables within each function. This separate storage space means that the <span class="docEmphasis">scope</span>, or where a variable's value can be accessed, is the local storage of the function. <a class="docLink" href="#learnphpmysql-CHP-3-FIG-3">Figure 3-3</a> demonstrates how there are distinct storage areas for a function's variables.</p>
<a name="learnphpmysql-CHP-3-FIG-3"></a><p><center>
<h5 class="docFigureTitle">Figure 3-3. The $age variable has a separate value outside of the birthday function's variable storage area</H5>
<img border="0" alt="" id="195131084199" width="515" height="211" SRC="images/learnphpmysql_0303.jpg">
</center></p><br>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-7">Example 3-7</a> shows how the variable you use outside of the function isn't changed by the code within the function. Don't worry too much about understanding how the function works, as it has its own set of unique variables.</P>
<a name="learnphpmysql-CHP-3-EX-7"></a><H5 id="title-IDAZW1ME" class="docExampleTitle">Example 3-7. The default handling of variable scope</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php

// define a function
function birthday(){
    // Set age to 1
    $age = 1;
}

// Set age to 30
<span class="docEmphStrong">$age = 30;</span>

// Call the function
birthday();

// Display the age
echo $age;

?&gt;
</pre><br>

</TD></tr></table></p>
<p class="docText">This displays:</P>
<pre>
30
</pre><br>

<p class="docText">Although calling the function <tt>birthday</tt> assigns 1 to the variable <tt>$age</tt>, it's not accessing the same variable that was defined on the main level of the program. Therefore, when you print <tt>$age</tt>, you see the original value of 30. The bolded part of the code is what is seen when <tt>$age =</tt> is printed, because <tt>$age</tt> in <tt>birthday</tt> is a separate variable.</p>
<p class="docText">All of this is great, but if you really want to access or change the variable <tt>$age</tt> that was created outside the <tt>birthday</tt> function, you would use a global<a name="IDX-CHP-3-0067"></a> 
<a name="IDX-CHP-3-0068"></a> 
 variable.</P>
<a name="learnphpmysql-CHP-3-SECT-2.1.3.1"></a>
<h5 id="title-IDAQY1ME" class="docSection4Title">3.2.1.3.1. Global variables</h5>
<a name="IDX-CHP-3-0069"></a> 

<p class="docText"><span class="docEmphasis">Global</span> variables allow you to cross the boundary between separate functions to access a variable's value. The <tt>global</tt> statement specifies that you want the variable to be the same variable everywhere, or globally. <a class="docLink" href="#learnphpmysql-CHP-3-FIG-4">Figure 3-4</a> shows how a global variable is accessible to everything.</p>
<a name="learnphpmysql-CHP-3-FIG-4"></a><p><center>
<H5 class="docFigureTitle">Figure 3-4. The global keyword creates one global variable called $age</H5>
<img border="0" alt="" id="195131084199" width="342" height="289" SRC="images/learnphpmysql_0304.jpg">
</center></p><br>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-8">Example 3-8</a> shows that use of a global variable can result in a change.</p>
<a name="learnphpmysql-CHP-3-EX-8"></a><h5 id="title-IDAZZ1ME" class="docExampleTitle">Example 3-8. Using a global variable changes the result</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php

// Define a function
function birthday(){
    // Define age as a global variable
    <span class="docEmphStrong">global $age;</span>

    // Add one to the age value
    $age = $age + 1;
}

// set age to 30
$age = 30;

// Call the function
birthday();

// Display the age
echo $age;

?&gt;
</pre><BR>

</TD></TR></table></p>
<p class="docText">This displays:</p>
<pre>
31
</pre><br>

<p class="docText">Global variables should be used sparingly, since it's easy to accidentally modify a variable by mistake. This kind of error can be very difficult to locate. Additionally, when we discuss functions in detail, you'll learn that you can send in values to functions when you call them and get values returned from them when they're done. That all boils down to the fact that you really don't have to use global variables.</p>
<p class="docText">If you want to use a variable in a specific function without losing the value each time the function ends, but you don't want to use a global variable, you would use a static<a name="IDX-CHP-3-0070"></a> 
<a name="IDX-CHP-3-0071"></a> 
 variable.</p>

<a name="learnphpmysql-CHP-3-SECT-2.1.3.2"></a>
<h5 id="title-IDA301ME" class="docSection4Title">3.2.1.3.2. Static variables</h5>
<a name="IDX-CHP-3-0072"></a> 

<p class="docText"><span class="docEmphasis">Static</span> variables provide a variable that isn't destroyed when a function ends. You can use the static variable value again the next time you call the function.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText"><span class="docEmphasis">Call</span> and <span class="docEmphasis">execute</span> mean the same thing.</p>
</td></TR></table></TD></tr></table></P><br>
<p class="docText">The easiest way to think about this is to realize that the variable is a global to just that function. A static keyword is used to dictate that the variable you're working with is static, as illustrated in <a class="docLink" href="#learnphpmysql-CHP-3-FIG-5">Figure 3-5</a>.</p>
<a name="learnphpmysql-CHP-3-FIG-5"></a><p><center>
<H5 class="docFigureTitle">Figure 3-5. The static variable creates a persistent storage space for $age in birthday</h5>
<img border="0" alt="" id="195131084199" width="549" height="274" SRC="images/learnphpmysql_0305.jpg">
</center></p><BR>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-3-EX-9">Example 3-9</a>, we use the <tt>static</tt> keyword to define these function variables.</p>
<a name="learnphpmysql-CHP-3-EX-9"></a><h5 id="title-IDAV21ME" class="docExampleTitle">Example 3-9. A static variable remembering its last value</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php

// Define the function

function birthday(){
    // Define age as a static variable
    static $age = 0;

    // Add one to the age value
    $age = $age + 1;

    // Print the static age variable
    echo "Birthday number $age&lt;br /&gt;";
}

// Set age to 30
$age = 30;

// Call the function twice
birthday();
birthday();

// Display the age
echo "Age: $age&lt;br /&gt;";

?&gt;
</pre><br>

</TD></TR></table></p>
<p class="docText">This displays:</p>
<pre>
Birthday number 1
Birthday number 2
Age: 30
</pre><br>

<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></TD><TD valign="top">
<p class="docText">The XHTML markup <tt>&lt;br /&gt;</tt> command is turned into line breaks when your browser displays the results.</P>
</td></tr></table></td></tr></table></p><br>
<p class="docText">The value of <tt>$age</tt> is now retained each time the <tt>birthday</tt> function is called. The value will stay around until the program quits. We've discussed two types of variables, but there's one more to discuss: <span class="docEmphasis">super global</span>.<a name="IDX-CHP-3-0073"></a> 
</p>

<a name="learnphpmysql-CHP-3-SECT-2.1.3.3"></a>
<h5 id="title-IDA231ME" class="docSection4Title">3.2.1.3.3. Super global variables</h5>
<a name="IDX-CHP-3-0074"></a> 

<p class="docText">PHP uses special variables called <span class="docEmphasis">super globals</span> to provide information about the PHP script's environment. These variables don't need to be declared as global; they are automatically available and provide important information beyond the script's environment, such as values from a user input.</p>
<p class="docText">Since PHP 4.01, the super globals are defined in <span class="docEmphasis">arrays</span>. Arrays are special collections of values that we'll discuss in <a class="docLink" href="learnphpmysql-CHP-7.html#learnphpmysql-CHP-7">Chapter 7</a>. The older super global variables<a name="IDX-CHP-3-0075"></a> 
<a name="IDX-CHP-3-0076"></a> 
 such as those starting with <tt>$HTTP_*</tt> that were not in arrays still exist, but their use is not recommended as they are less secure. <a class="docLink" href="#learnphpmysql-CHP-3-TABLE-1">Table 3-1</a> shows the existing arrays since PHP 4.01.</p>
<a name="learnphpmysql-CHP-3-TABLE-1"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 3-1. PHP super globals</h5></caption><colgroup span="2"><col><col></colgroup><thead><TR><th class="thead" scope="col" align="left"><p class="docText">Variable array name</p></th><th class="thead" scope="col" align="left"><p class="docText">Contents</p></th></TR></thead><TR><td class="docTableCell" align="left"><p class="docText">$GLOBALS</P></td><td class="docTableCell" align="left"><p class="docText">Contains any global variables that are accessible for the local script. The variable names are used to select which part of the array to access.</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText">$_SERVER</p></td><TD class="docTableCell" align="left"><p class="docText">Contains information about the web server environment.</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">$_GET</P></td><td class="docTableCell" align="left"><p class="docText">Contains information from <tt>GET</tt> requests (a form submission).</p></td></TR><tr><TD class="docTableCell" align="left"><p class="docText">$_POST</p></TD><TD class="docTableCell" align="left"><p class="docText">Contains information from <tt>POST</tt> requests (another type of form submission).</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">$_COOKIE</p></td><td class="docTableCell" align="left"><p class="docText">Contains inform from HTTP cookies.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">$_FILES</p></td><TD class="docTableCell" align="left"><p class="docText">Contains information from <tt>POST</tt> file uploads.</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">$_ENV</P></td><td class="docTableCell" align="left"><p class="docText">Contains information about the environment (Windows or Mac).</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText">$_REQUEST</p></td><TD class="docTableCell" align="left"><p class="docText">Contains information from user inputs. These values should not be trusted.</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">$_SESSION</P></td><td class="docTableCell" align="left"><p class="docText">Contains information from any variables registered in a session.</p></td></TR></table></p><BR>
<p class="docText">An example of a super global is <tt>PHP_SELF</tt>. This variable contains the name of the running script and is part of the <tt>$_SERVER</tt> array, as shown in <a class="docLink" href="#learnphpmysql-CHP-3-EX-10">Example 3-10</a>.</p>
<a name="learnphpmysql-CHP-3-EX-10"></a><H5 id="title-IDAKE2ME" class="docExampleTitle">Example 3-10. PHP_SELF being used with a file called test.php</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
echo $_SERVER["PHP_SELF"];
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">This outputs:</p>
<pre>
/test.php
</pre><br>

<p class="docText">This variable is especially useful, as it can be used to call the current script again when processing a form. Super global variables provide a convenient way to access information about a script's environment from server settings to user inputted data. Now that you've got a handle on variables and scope, we can talk about what types of information variables hold.</p>



<a name="learnphpmysql-CHP-3-SECT-2.2"></a>
<h4 id="title-IDA2E2ME" class="docSection2Title">3.2.2. Strings</h4>
<p class="docText">Variables can hold more than just numbers.<a name="IDX-CHP-3-0077"></a> 
 They can hold characters and <span class="docEmphasis">strings</span>,<a name="IDX-CHP-3-0078"></a> 
 or an ordered list of characters. <a class="docLink" href="#learnphpmysql-CHP-3-FIG-6">Figure 3-6</a> demonstrates how an ordered list of characters becomes a string.</p>
<a name="learnphpmysql-CHP-3-FIG-6"></a><p><center>
<h5 class="docFigureTitle">Figure 3-6. How individual characters form a string</H5>
<img border="0" alt="" id="195131084199" width="513" height="179" SRC="images/learnphpmysql_0306.jpg">
</center></p><br>
<p class="docText">A string can be used directly in a function call or it can be stored in a variable. In <a class="docLink" href="#learnphpmysql-CHP-3-EX-11">Example 3-11</a>, we create the exact same string twice: first we store it in a variable, and then we place the string directly into a function.</P>
<a name="learnphpmysql-CHP-3-EX-11"></a><H5 id="title-IDAJG2ME" class="docExampleTitle">Example 3-11. Working with strings</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$my_string = "Margaritaville - Suntan Oil Application!";
echo "Margaritaville - Suntan Oil Application!";
?&gt;
</pre><br>

</TD></tr></table></p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-3-EX-12">Example 3-12</a>, the first string is stored in the variable <tt>$my_string</tt>, while the second string is used in the <tt>echo</tt> function and <span class="docEmphasis">isn't</span> stored. Remember to save your strings into variables if you plan on using them more than once!</P>
<p class="docText">Strings are flexible. You can even insert variables into string definitions when using double quotes to start and end your string, as shown in <a class="docLink" href="#learnphpmysql-CHP-3-EX-12">Example 3-12</a>. Using a single quote to start and end your string does not allow a variable to be placed in the string.</p>
<a name="learnphpmysql-CHP-3-EX-12"></a><h5 id="title-IDAIH2ME" class="docExampleTitle">Example 3-12. Using a variable in a string definition</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$my_string = "Margaritaville - Suntan Oil Application!";
echo "Time for $my_string";
?&gt;
</pre><br>

</TD></TR></table></p>
<p class="docText">This example displays "Time for Margaritaville - Suntan Oil Application!" Double quotes are used in the above string, but single quotes or apostrophes can both be used as long as you won't be inserting variable values; see <a class="docLink" href="#learnphpmysql-CHP-3-EX-13">Example 3-13</a>.</p>
<a name="learnphpmysql-CHP-3-EX-13"></a><h5 id="title-IDAZH2ME" class="docExampleTitle">Example 3-13. Single quotes used in a string assignment</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
$my_string = 'Margaritaville - Suntan Oil Application!';
echo $my_string;
?&gt;
</pre><br>

</TD></TR></table></P>
<p class="docText">Remember, if you want to use a single quote within a string marked with single quotes, you have to <span class="docEmphasis">escape</span> the single quote with a backslash (<tt>\</tt>). Double quotes allow the use of many special escaped characters that you can't use with a single quote string, such as an apostrophe. If you had escaped an apostrophe with a backslash in a double-quoted string, the backslash would show up when you output the string.</p>
<a name="learnphpmysql-CHP-3-SECT-2.2.1"></a>
<h5 id="title-IDAMI2ME" class="docSection3Title">3.2.2.1. Special characters in strings</h5>
<a name="IDX-CHP-3-0079"></a> 

<p class="docText">Tab, newline, and carriage returns are all examples of extra, yet ignorable, whitespace (see <a class="docLink" href="#learnphpmysql-CHP-3-EX-14">Example 3-14</a>). If you are writing to a file, a valuable tool is an escaped character. One downside of using the apostrophe to start and end a string is that you can't include a variable. This leads us to be careful about using HTML markup or any other string that includes quotes.</p>
<a name="learnphpmysql-CHP-3-EX-14"></a><h5 id="title-IDA4I2ME" class="docExampleTitle">Example 3-14. Various special characters in string assignments</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<a name="IDX-CHP-3-0080"></a> 
<a name="IDX-CHP-3-0081"></a> 

<pre>
&lt;?php
$newline = "A newline is \n";
$return = "A carriage return is \r";
$tab = "A tab is \t";
$dollar = "A dollar sign is \$";
$doublequote = "A double-quote is \"";
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">The <tt>echo</tt> function uses quotes to define the start and end of a string, so you must use one of the following tactics if your string contains quotations:</p>
<UL><li><p class="docList">Don't use quotes inside your string.</p></LI><LI><p class="docList">Escape quotes within the string with a slash. To escape a quote, just place a slash directly before the quotation mark; i.e., <tt>\"</tt>.</p></LI><li><p class="docList">Use single quotes (apostrophes) for quotes inside your string.</p></li><LI><p class="docList">Start and end your string with apostrophes.</p></li></UL>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-3-EX-15">Examples 3-15</a> and <a class="docLink" href="#learnphpmysql-CHP-3-EX-16">3-16</a>, the wrong and right use of the <tt>echo</tt> function is demonstrated.</p>
<a name="learnphpmysql-CHP-3-EX-15"></a><h5 id="title-IDA0K2ME" class="docExampleTitle">Example 3-15. Using echo with special characters</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
// This won't work because of the quotes around specialH2!
echo "&lt;h2 class="specialH2"&gt;Margaritaville!&lt;/h2&gt;";
?&gt;
specialH2
</pre><br>

</TD></TR></table></p>
<p class="docText">In the first <tt>echo</tt> example, we forgot to escape the double quotes that surround the <tt>specialH2</tt>, which is HTML text. Attempting to display this page produces the error:</p>
<pre>
<b>Parse error:</b> parse error, unexpected T_STRING, expecting ','
or ';' in <b>/home/www/html/oreilly/ch3/parse.php</b> on line <B>3</b>
</pre><br>

<p class="docText">If you see that error, start by checking your single and double quotes to make sure they all match up correctly, as in <a class="docLink" href="#learnphpmysql-CHP-3-EX-16">Example 3-16</a>.</P>
<a name="learnphpmysql-CHP-3-EX-16"></a><h5 id="title-IDA1L2ME" class="docExampleTitle">Example 3-16. Correct escaping of special characters</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
// OK because we used single quotes
echo "&lt;h2 class=\"specialH2\"&gt;Margaritaville!&lt;/h2&gt;";
echo '&lt;h2 class="specialH2"&gt;Margaritaville!&lt;/h2&gt;';
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-16">Example 3-16</a> <span class="docEmphasis">escapes</span> quotations by placing a slash in front of each one (<tt>\"</tt>). The slash tells PHP that you want the quotation to be used within the string and <span class="docEmphasis">not</span> ending <tt>echo</tt>'s string. You can also use an apostrophe (<tt>'</tt>) to mark the beginning and end of a string.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><td width="60" valign="top"><img src="images/warning_yellow.jpg" width="51" height="36" alt=""></td><td valign="top">
<p class="docText">If you use an apostrophe or single quote to define your string, double quotes don't need to be escaped. However, you can't include variables when using single quotes.</p>
</TD></tr></table></td></TR></table></P><br>
<p class="docText">You'll find that when you're working with strings, you'll want to combine them. This is actually like working with shorthand instead of writing out each and every word.</P>

<a name="learnphpmysql-CHP-3-SECT-2.2.2"></a>
<h5 id="title-IDA5M2ME" class="docSection3Title">3.2.2.2. Comparing strings</h5>
<a name="IDX-CHP-3-0082"></a> 

<p class="docText">PHP has functions to compare strings that aren't exactly alike. For example, you may want to consider "Bill" to be the same as "BILL," ignoring the case of the string.</p>
<p class="docText">Use <tt>strcmp (</tt><tt><I>string1, string2</i></tt><tt>)</tt> to compare two strings including the case. The return value is 0 if the two strings have the same text. Any nonzero value indicates they are not the same.</p>
<p class="docText">Use <tt>strcasecmp (</tt><tt><I>string1, string2</i></tt><tt>)</tt> to compare two strings without comparing<a name="IDX-CHP-3-0083"></a> 
<a name="IDX-CHP-3-0084"></a> 
 the case. The return value is 0 if the two strings have the same text. Any nonzero value indicates they are not the same.</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-17">Example 3-17</a> compares "Bill" to "BILL" without considering the case.</P>
<a name="learnphpmysql-CHP-3-EX-17"></a><h5 id="title-IDAMO2ME" class="docExampleTitle">Example 3-17. Using strcasecmp to compare two strings</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php

$name1 = "Bill";
$name2 = "BILL";

$result = strcasecmp($name1, $name2);

if (!$result){
   echo "They match.";
}

?&gt;
</pre><BR>

</td></tr></table></p>
<p class="docText">This returns:</p>
<pre>
They match.
</pre><BR>



<a name="learnphpmysql-CHP-3-SECT-2.3"></a>
<h4 id="title-IDA1O2ME" class="docSection2Title">3.2.3. Concatenation</H4>
<a name="IDX-CHP-3-0085"></a> 
<a name="IDX-CHP-3-0086"></a> 

<p class="docText">Concatenation combines one or more text strings and variables, as shown in <a class="docLink" href="#learnphpmysql-CHP-3-EX-18">Example 3-18</a>. When performing this combination, you save yourself the hassle of creating numerous <tt>echo</tt> statements, or in other words, you build up a string and use it.</p>
<a name="learnphpmysql-CHP-3-EX-18"></a><H5 id="title-IDAVP2ME" class="docExampleTitle">Example 3-18. Concatenating strings together</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$my_string = "Hello Max. My name is: ";
$newline = "&lt;br /&gt;";
echo $my_string . "Paula" . $newline;
echo "Hi, I'm Max. Who are you? " . $my_string . $newline;
echo "Hi, I'm Max. Who are you? " . $my_string . "Paula";
//The last line is the same as echo "Hi, I'm max. Who are you? $my_string Paula";
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">The output of your code looks like <a class="docLink" href="#learnphpmysql-CHP-3-FIG-7">Figure 3-7</a> in your browser window.</p>
<a name="learnphpmysql-CHP-3-FIG-7"></a><p><center>
<h5 class="docFigureTitle">Figure 3-7. Concatenation output</h5>
<img border="0" alt="" id="195131084199" width="549" height="149" SRC="images/learnphpmysql_0307.jpg">
</center></p><br>
<p class="docText">Variables and text strings are joined together with a period (<tt>.</tt>). This can be done multiple times, as shown in <a class="docLink" href="#learnphpmysql-CHP-3-FIG-8">Figure 3-8</a>.</p>
<a name="learnphpmysql-CHP-3-FIG-8"></a><p><center>
<H5 class="docFigureTitle">Figure 3-8. How strings come together with concatenation</h5>
<img border="0" alt="" id="195131084199" width="472" height="187" SRC="images/learnphpmysql_0308.jpg">
</center></p><BR>
<p class="docText">Since your time is finite, tying strings and variables together helps you create dynamic web sites faster.</P>
<a name="learnphpmysql-CHP-3-SECT-2.3.1"></a>
<h5 id="title-IDANR2ME" class="docSection3Title">3.2.3.1. Combining strings with other types</H5>
<a name="IDX-CHP-3-0087"></a> 
<a name="IDX-CHP-3-0088"></a> 
<a name="IDX-CHP-3-0089"></a> 

<p class="docText">If you combine a string with another data type, such as a number, the result is also a string, as shown in <a class="docLink" href="#learnphpmysql-CHP-3-EX-19">Example 3-19</a>.</p>
<a name="learnphpmysql-CHP-3-EX-19"></a><h5 id="title-IDALS2ME" class="docExampleTitle">Example 3-19. Combining a string and a number</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$str = "This is an example of ". 3 ." in the middle of a string.";
echo $str;
?&gt;
</pre><BR>

</td></tr></table></P>
<p class="docText">This displays:</p>
<pre>
This is an example of 3 in the middle of a string.
</pre><br>

<p class="docText"><tt>$str</tt> contains a string even though a number was inserted into the middle.</p>


<a name="learnphpmysql-CHP-3-SECT-2.4"></a>
<h4 id="title-IDA4S2ME" class="docSection2Title">3.2.4. Constants</H4>
<p class="docText">You can define <tt>constants</tt><a name="IDX-CHP-3-0090"></a> 
 in your program. A constant, like its name implies, cannot change its value during the execution of your program. It's defined using the <tt>define</tt> function, which takes the name of the constant as the first parameter and the values as the second parameter. The definition of a constant is global and can be defined as any simple (scalar) data type such as a string or a number. You can get the value of a constant by simply specifying its name; see <a class="docLink" href="#learnphpmysql-CHP-3-EX-20">Example 3-20</a>. Unlike how you handle variables, you should not put the dollar sign (<tt>$</tt>) before a constant. A cool thing you do with constants is using the function <tt>constant(</tt><tt><I>name</i></tt><tt>)</tt> to return a constant's value when the constant's name is determined dynamically. Or you could use <tt>get_defined_constants</tt> to return a list (as an array) of all your defined constants. If you're unsure about the arguments to a function, you can search the PHP site at <a class="docLink" target="_blank" href="http://www.php.net">http://www.php.net</a> to find function parameters and return values.</p>
<p class="docText">These are the differences between constants and variables:</p>
<ul><LI><p class="docList">Constants do not have a dollar sign (<tt>$</tt>) before them.</p></LI><li><p class="docList">Constants can only be defined using the <tt>define</tt> function, not by simple assignment.</P></LI><LI><p class="docList">Constants are defined and accessed globally.</p></li><li><p class="docList">Constants cannot be redefined or undefined once they have been set.</p></li><li><p class="docList">Constants can only evaluate to scalar values.</p></li></ul>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-20">Example 3-20</a> demonstrates how to use a constant in your program.</p>
<a name="learnphpmysql-CHP-3-EX-20"></a><h5 id="title-IDACV2ME" class="docExampleTitle">Example 3-20. Using a constant in your program</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
define("HELLO", "Hello world!");
echo HELLO; // outputs "Hello world."
?&gt;
</pre><br>

</td></TR></table></P>
<p class="docText">This outputs:</p>
<pre>
Hello world!
</pre><BR>

<p class="docText">Constants are useful for values that you need to make sure don't change, such as a configuration file location.</p>
<p class="docText">If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a stringfor example, <tt>CONSTANT</tt> as opposed to <tt>"CONSTANT"</tt>. If the define line of <a class="docLink" href="#learnphpmysql-CHP-3-EX-20">Example 3-20</a> is commented out, the output becomes:</p>
<pre>
HELLO
</pre><br>

<a name="learnphpmysql-CHP-3-SECT-2.4.1"></a>
<H5 id="title-IDA5V2ME" class="docSection3Title">3.2.4.1. Predefined constants</h5>
<a name="IDX-CHP-3-0091"></a> 

<p class="docText">PHP provides a few constants that are predefined<a name="IDX-CHP-3-0092"></a> 
<a name="IDX-CHP-3-0093"></a> 
 similar to the way we have some super globals. Examples of these include <tt>__FILE__</tt>, which returns the name of the PHP file that's being executed, and <tt>__LINE__</tt>, which returns the line number in that file. They can be handy for generating an error message as they tell you where in your code the error occurred, as shown in <a class="docLink" href="#learnphpmysql-CHP-3-EX-21">Example 3-21</a>.</p>
<a name="learnphpmysql-CHP-3-EX-21"></a><H5 id="title-IDAAX2ME" class="docExampleTitle">Example 3-21. Echoing the line and file predefined constants for a script called predefined_constants.php</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
echo "Executing line ".  __LINE__ . " of PHP script " . __FILE__ . '.';
?&gt;
</pre><br>

</td></tr></table></P>
<p class="docText">This returns:</P>
<pre>
Executing line 2 of PHP script /home/www/html/oreilly/ch3/predefined_constants.php.
</pre><br>

<p class="docText">The path to your script may be different than the example. On Windows, it's likely to be <span class="docEmphasis">C:\Program Files\Apache Group\htdocs\c3</span>.</p>


<a name="learnphpmysql-CHP-3-SECT-2.5"></a>
<h4 id="title-IDAUX2ME" class="docSection2Title">3.2.5. Doing Math</h4>
<a name="IDX-CHP-3-0094"></a> 
<a name="IDX-CHP-3-0095"></a> 

<p class="docText">Variables can hold numbers, too, and it's useful to perform mathematical operations on those numbers. All fundamental mathematical functions are available using PHP. You may feel like you're back in middle school algebra, but the basic functions are just like they were then: adding, subtracting, multiplying, and dividing. In <a class="docLink" href="#learnphpmysql-CHP-3-EX-22">Example 3-22</a>, the divide (<tt>/</tt>) operator calculates the percentage from its operands sunny days and total days in a year to get a percentage of approximately 82 percent.</P>
<a name="learnphpmysql-CHP-3-EX-22"></a><h5 id="title-IDAQY2ME" class="docExampleTitle">Example 3-22. PHP mathematical function usage</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><TD>

<pre>
&lt;?php&gt;
$sunny_days=300;
$Margaritaville_sunny_days_ratio=$sunny_days/365;
echo $Margaritaville_sunny_days_ratio;
?&gt;
</pre><BR>

</td></tr></table></p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-3-FIG-9">Figure 3-9</a>, the 82 percent outcome from our example code displays in your browser window.</p>
<a name="learnphpmysql-CHP-3-FIG-9"></a><p><center>
<h5 class="docFigureTitle">Figure 3-9. Mathematical operation output</h5>
<img border="0" alt="" id="195131084199" width="549" height="171" SRC="images/learnphpmysql_0309.jpg">
</center></p><br>
<p class="docText">PHP also supports the mathematical operations listed in <a class="docLink" href="#learnphpmysql-CHP-3-TABLE-2">Table 3-2</a>.</p>
<a name="learnphpmysql-CHP-3-TABLE-2"></a><p><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 3-2. The basic mathematical operators</h5></caption><colgroup span="4"><col><col><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Mathematical operator</P></th><th class="thead" scope="col" align="left"><p class="docText">Name</p></th><th class="thead" scope="col" align="left"><p class="docText">Example</p></th><th class="thead" scope="col" align="left"><p class="docText">Result</P></th></TR></thead><tr><TD class="docTableCell" align="left"><p class="docText">+</p></td><td class="docTableCell" align="left"><p class="docText">Addition</P></td><td class="docTableCell" align="left"><p class="docText">2+2</P></td><td class="docTableCell" align="left"><p class="docText">4</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">-</P></TD><td class="docTableCell" align="left"><p class="docText">Subtraction</p></td><td class="docTableCell" align="left"><p class="docText">2-1</P></td><TD class="docTableCell" align="left"><p class="docText">1</p></TD></TR><TR><td class="docTableCell" align="left"><p class="docText">*</p></td><td class="docTableCell" align="left"><p class="docText">Multiplication</p></td><td class="docTableCell" align="left"><p class="docText">2*2</p></td><td class="docTableCell" align="left"><p class="docText">4</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">/</p></td><TD class="docTableCell" align="left"><p class="docText">Division</P></td><TD class="docTableCell" align="left"><p class="docText">2/2</p></td><td class="docTableCell" align="left"><p class="docText">1</P></td></tr><TR><td class="docTableCell" align="left"><p class="docText">%</p></TD><td class="docTableCell" align="left"><p class="docText">Modulo (remainder)</p></td><td class="docTableCell" align="left"><p class="docText">2%1</P></TD><td class="docTableCell" align="left"><p class="docText">0</p></td></tr></table></P><br>
<p class="docText">The operators can take whole numbers or decimal numbers as their input.</P>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><TR><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/warning_yellow.jpg" width="51" height="36" alt=""></td><td valign="top">
<p class="docText">Use caution when dividing to avoid dividing a number by zero, as this generates an infinite result and displays this PHP<a name="IDX-CHP-3-0096"></a> 
 warning: "Warning: Division by zero."</p>
</td></tr></table></td></tr></table></p><br>
<p class="docText">Of course, you can do all sorts of complex mathematical operations such as trigonometry, and there is a specific order in which the math operators are applied, but we'll discuss those in the next chapter. You can also use <a class="docLink" target="_blank" href="http://www.php.net">http://www.php.net</a> by entering <tt>Math</tt> into their search tool, which provides a link to <a class="docLink" target="_blank" href="http://us3.php.net/manual/en/ref.math.php">http://us3.php.net/manual/en/ref.math.php</a> that gives you a detailed listing and usage of all of the math functions.</p>
<a name="learnphpmysql-CHP-3-SECT-2.5.1"></a>
<h5 id="title-IDAG52ME" class="docSection3Title">3.2.5.1. Combined assignment</h5>
<a name="IDX-CHP-3-0097"></a> 
<a name="IDX-CHP-3-0098"></a> 

<p class="docText">Combined assignment operators provide a shortcut for performing two common tasks at the same time. They combine reading a variable, performing an operation on it, and placing the result back in the same variable. The operations are mostly mathematical but can also include other operators like concatenation.</p>
<p class="docText">Combined assignment operators take the form the arithmetic operator directly followed by an equals sign (<tt>=</tt>). For example, the statement:</P>
<pre>
$counter=$counter+1;
</pre><br>

<p class="docText">is equivalent to:</p>
<pre>
$counter+=1;
</pre><BR>

<p class="docText">Which is shorthand for taking the value in <tt>$counter</tt>, adding one to it, and then saving the result back in <tt>$counter</tt>.</P>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-TABLE-3">Table 3-3</a> lists the most common combined assignment operators.</p>
<a name="learnphpmysql-CHP-3-TABLE-3"></a><P><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 3-3. Combined assignment operators</h5></caption><colgroup span="3"><col><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Combined operation</P></th><th class="thead" scope="col" align="left"><p class="docText">Operation</p></th><th class="thead" scope="col" align="left"><p class="docText">Produces</p></th></TR></thead><tr><td class="docTableCell" align="left"><p class="docText">$num+=y</P></td><td class="docTableCell" align="left"><p class="docText">Addition</p></td><TD class="docTableCell" align="left"><p class="docText">$num=$num+y</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">$num -=y</P></td><TD class="docTableCell" align="left"><p class="docText">Subtraction</p></TD><TD class="docTableCell" align="left"><p class="docText">$num=$num-y</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">$num *=y</p></td><td class="docTableCell" align="left"><p class="docText">Multiplication</p></td><td class="docTableCell" align="left"><p class="docText">$num=$num*y</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">$num /=y</p></td><TD class="docTableCell" align="left"><p class="docText">Division</P></td><TD class="docTableCell" align="left"><p class="docText">$num=$num/y</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">$num.= "y"</p></TD><td class="docTableCell" align="left"><p class="docText">Concatenation</p></TD><td class="docTableCell" align="left"><p class="docText">$string=$string."y"</p></td></tr></table></P><BR>
<p class="docText">You'll find that these operators are very handy when creating your dynamic web pages. They'll also be used frequently in our examples. They have the added benefit of reducing the chance that you'll have a typo in your variable name, since you need to specify the variable name once only.</p>
<p class="docText">Along the same lines as combined operators comes a shorthand method for adding one or subtracting one from a variable.</p>

<a name="learnphpmysql-CHP-3-SECT-2.5.2"></a>
<h5 id="title-IDAUE3ME" class="docSection3Title">3.2.5.2. Autoincrement and autodecrement</h5>
<a name="IDX-CHP-3-0099"></a> 
<a name="IDX-CHP-3-0100"></a> 

<p class="docText">It's very common when writing your code to ether increment<a name="IDX-CHP-3-0101"></a> 
<a name="IDX-CHP-3-0102"></a> 
 or decrement<a name="IDX-CHP-3-0103"></a> 
<a name="IDX-CHP-3-0104"></a> 
 a variable by one. It's so common that PHP has a special shortcut for doing it. The autoincrement operator is <tt>++</tt> and is used like this:</P>
<pre>
$counter++;
</pre><br>

<p class="docText">What we did was:</P>
<pre>
$counter+=1;
</pre><br>

<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-23">Example 3-23</a> adds one to <tt>$counter</tt>.</P>
<a name="learnphpmysql-CHP-3-EX-23"></a><H5 id="title-IDAOG3ME" class="docExampleTitle">Example 3-23. Using autoincrement to add to a variable</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$counter=1;
$counter++;
echo $counter
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">This produces:</p>
<pre>
2
</pre><br>

<p class="docText">The same concept applies to the automatic decrement operator, <tt>--</tt>.</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-3-EX-24">Example 3-24</a> subtracts one from <tt>$counter</tt>.</p>
<a name="learnphpmysql-CHP-3-EX-24"></a><h5 id="title-IDAJH3ME" class="docExampleTitle">Example 3-24. Using the autodecrement operator</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
$counter=1;
$counter--;
echo $counter
?&gt;
</pre><br>

</TD></TR></table></p>
<p class="docText">This produces:</P>
<pre>
0
</pre><br>

<p class="docText">This notation is used frequently when doing repetitive tasks to keep track of how many times you've done them.</p>

<a name="learnphpmysql-CHP-3-SECT-2.5.3"></a>
<h5 id="title-IDAZH3ME" class="docSection3Title">3.2.5.3. Pre-increment and -decrement</H5>
<p class="docText">If you're incrementing or decrementing at the same time that you're also comparing the value of the variable, such as in a <tt>for</tt> or <tt>while</tt> loop, a pre-increment or -decrement can affect the value that's used for the comparison. When using the pre- operations, the value changes before the comparison, which is different than the de facto post-processing.</p>
<p class="docText">For example:</p>
<pre>
--$counter;
</pre><BR>

<p class="docText">or:</p>
<pre>
++$counter;
</pre><br>

<p class="docText">Both of the operators still change the value of the counter variable, but they change the value sooner. If you are using that variable in a test, you'll see the current value before the change. We'll talk more about testing the values of variable executing blocks of code repetitively in the next chapter. <a class="docLink" href="#learnphpmysql-CHP-3-EX-25">Example 3-25</a> shows how these operators work.</P>
<a name="learnphpmysql-CHP-3-EX-25"></a><h5 id="title-IDAZI3ME" class="docExampleTitle">Example 3-25. Using pre- and post-increment</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
$test=1;
echo "Preincrement: ".(++$test);
echo "&lt;BR&gt;";
echo "Value afterwords: ".$test;
echo "&lt;BR&gt;";
$test=1;
echo "Postincrement: ".($test++);
echo "&lt;BR&gt;";
echo "Value afterwords: ".$test;
?&gt;
</pre><BR>

</td></tr></table></p>
<p class="docText">This produces:</p>
<pre>
Preincrement: 2
Value afterwords: 2
Postincrement: 1
Value afterwords: 2
</pre><BR>

<p class="docText">Notice that in <a class="docLink" href="#learnphpmysql-CHP-3-EX-25">Example 3-25</a>, the value after a post- or pre-increment is always 2. When using the pre-increment, the value is 2 in the <tt>echo</tt> statement that contains the combined operator.</p>
<p class="docText">In this chapter, you've learned about the basic concepts for writing PHP scripts. You've introduced variables that can remember information while our scripts execute. You know how to store values in variables and access those values. You don't have to worry about specifying data types, because PHP attempts to convert types automatically. You've also learned how to do basic mathematical operations and the shortcuts for the most common combined assignment operators.</P>
<p class="docText">These concepts will form the basis for the rest of what you learn about PHP programming, including building expressions.</p>
<p class="docText">The next chapter will introduce more complicated PHP code such as arrays, including looping and conditional logic. After that, we'll be able to jump into MySQL and how it operates as a database.</P>



</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-3-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
